from threading import Thread
import time

def start_keep_alive_task(app, engine_dict):
    keep_alive_thread = Thread(target=keep_db_alive, args=(app, engine_dict))
    keep_alive_thread.start()

def keep_db_alive(app, engine_dict):
    with app.app_context():
        while True:
            try:
                engine = engine_dict['mysql-localhost']
                with engine.connect() as conn:
                    conn.execute("SELECT 1")
                # print("Keep-alive task executed")
            except Exception as e:
                print(f"Error in keep-alive task: {e}")
            time.sleep(60)  # 每分钟执行一次
